package org.sundatagroup.aurora.core.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.sundatagroup.aurora.core.entity.model.ParticleModel;
import org.sundatagroup.aurora.core.entity.particle.Particle;

/**
 * <p>封装 {@link Particle} 的数据操作的 DAO 层
 * </p>
 * 
 * @author Zdream
 * @date 2018-06-08
 * @since 0.2.0
 */
@Repository
public class ParticleDao {
	
    @Autowired
    MongoTemplate mongoOps;

    //private final static Logger LOGGER = LoggerFactory.getLogger(ParticleDao.class);

    /**
     * 查询工程域的默认粒子配置
     */
    public ParticleModel findByProjectScope(int pid) {
    	ParticleModel pm;
        Query query = Query.query(Criteria.where("rule.project").is(pid)
        		.and("rule.module").is(null)
        		.and("rule.index").is(null));
        if ((pm = mongoOps.findOne(query, ParticleModel.class)) == null) {
            return null;
        } else {
            return pm;
        }
    }

    /**
     * 查询指定工程下的某个指定模块的粒子配置
     */
    public ParticleModel findByModuleInProject(int pid, int mid) {
    	ParticleModel pm;
        Query query = Query.query(Criteria.where("rule.project").is(pid)
        		.and("rule.module").is(mid)
        		.and("rule.index").is(null));
        if ((pm = mongoOps.findOne(query, ParticleModel.class)) == null) {
            return null;
        } else {
            return pm;
        }
    }

    /**
     * 查询指定工程下第几个模块的粒子配置
     */
    public ParticleModel findByIndexInProject(int pid, int index) {
    	ParticleModel pm;
        Query query = Query.query(Criteria.where("rule.project").is(pid)
        		.and("rule.module").is(null)
        		.and("rule.index").is(index));
        if ((pm = mongoOps.findOne(query, ParticleModel.class)) == null) {
            return null;
        } else {
            return pm;
        }
    }
    
    /**
     * 查询指定工程下的所有例子配置
     * @param pid
     *   project.pid
     * @return
     */
    public List<ParticleModel> findAllByProject(int pid) {
    	Query query = Query.query(Criteria.where("rule.project").is(pid));
    	return mongoOps.find(query, ParticleModel.class);
    }

}
